﻿using KPIS.GERP.Models.Class;
using System;
using System.Web.Mvc;
using Resources;
using KPIS.GERP.Models.ICM;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
    public class PurchaseOrderItemController : Controller
    {
        public ActionResult Index(int PoId)
        {
            try
            {
                using (var service = new PurchaseOrderItemService.PurchaseOrderItemServiceClient())
                {
                    var model = service.Select(new PurchaseOrderItem { PoId = PoId }, new[] { "PurchaseOrder", "BidderQuotation.PurchaseRequestItem.MasResourceItem" });                  
                    ViewBag.PoId = PoId;
                    return View(model);
                }
                
            }catch(Exception ex){
                ModelState.AddModelError(ErrorLocalization.Error, ErrorLocalization.ErrorWhileDeleteData);
                GerpLog.Error(ErrorLocalization.ErrorWhileDeleteData + ", " + ex.Message);
                return View(new PurchaseOrderItem());
            }
        }

        //public ActionResult details(int id)
        //{
        //    var service = new PurchaseOrderItemService.PurchaseOrderItemServiceClient();
        //    var model = service.Select(new PurchaseOrderItem { pritemid = id }, new[] {
        //        "purchaseorder.maspurchasetype"
        //        , "purchaseorder.yeartype"
        //        //, "projectoperationexpense.projectoperation"
        //        //, "projectoperationexpense.projectexpense.masbudgetexpense"
        //        , "masresourceitem.masresourcesubgroup.masresourcegroup.masresourcetype"
        //        , "masresourceitem.masunit" }).firstordefault();

        //    ViewBag.pctypeid = model.purchaseorder.pctypeid;
        //    ViewBag.pctypename = model.purchaseorder.maspurchasetype.pctypename;
        //    ViewBag.yeartypename = model.purchaseorder.yeartype.yeartypename;
        //    ViewBag.bgyear = model.purchaseorder.bgyear;
        //    ViewBag.beditable = model.purchaseorder.recordstatus == kpis.gerp.models.sys.recordstatus.waitstatus || model.purchaseorder.recordstatus == kpis.gerp.models.sys.recordstatus.returnedstatus;

        //    return View(model);
        //}

        
        //public ActionResult Edit(int Id)
        //{
        //    var service = new PurchaseOrderItemService.PurchaseOrderItemServiceClient();
        //    var model = service.Select(new Models.ICM.PurchaseOrderItem { PrItemId = Id }, new[] {
        //        "PrePurchaseOrder.MasPurchaseType"
        //        , "PrePurchaseOrder.YearType"
        //        //, "ProjectOperationExpense.ProjectOperation"
        //        //, "ProjectOperationExpense.ProjectExpense.MasBudgetExpense"
        //        , "MasResourceItem.MasResourceSubGroup.MasResourceGroup.MasResourceType"
        //        , "MasResourceItem.MasUnit" }).FirstOrDefault();

        //    ViewBag.PcTypeId = model.PurchaseOrder.PcTypeId;
        //    ViewBag.PcTypeName = model.PurchaseOrder.MasPurchaseType.PcTypeName;
        //    ViewBag.YearTypeName = model.PurchaseOrder.YearType.YearTypeName;
        //    ViewBag.BgYear = model.PurchaseOrder.BgYear;

        //    return View(model);
        //}

        //[HttpPost, ValidateAntiForgeryToken]
        //public ActionResult Edit(int Id, PurchaseOrderItem model)
        //{
        //    try
        //    {
        //        if (ModelState.IsValid)
        //        {
        //            model.MasResourceItem = null;//model.ResItemId.Value;
        //            //model.ProjectOperationExpense = null;//model.ProjOperExpId.Value;

        //            var service = new PurchaseOrderItemService.PurchaseOrderItemServiceClient();
        //            service.Update(model, Class.GimsMembership.CurrentEmpId);

        //            return RedirectToAction("Details", new { Id = model.PrItemId });
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }

        //    return View(model);
        //}


        //public ActionResult Delete(int Id, int? PrId)
        //{
        //    try
        //    {
        //        var service = new PurchaseOrderItemService.PurchaseOrderItemServiceClient();
        //        service.Delete(Id, Class.GimsMembership.CurrentEmpId);
        //        service.Close();
        //        return RedirectToAction("Index", new { PrId = PrId });
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        //[HttpPost, ValidateAntiForgeryToken]
        //public ActionResult DeleteAll(int[] deleteIds)
        //{
        //    if (deleteIds == null)
        //        return Json(new { success = true });

        //    try
        //    {
        //        var service = new PurchaseOrderItemService.PurchaseOrderItemServiceClient();
        //        foreach (int delete in deleteIds)
        //        {
        //            service.Delete(delete, Class.GimsMembership.CurrentEmpId);
        //        }

        //        ModelState.AddModelError(ErrorLocalization.Error, string.Format("ลบข้อมูลไปทั้งหมด {0} รายการ", deleteIds.Length.ToString("#,##0")));
        //        return Json(new { success = true });
        //    }
        //    catch (Exception ex)
        //    {
        //        ModelState.AddModelError(ErrorLocalization.Error, ErrorLocalization.ErrorWhileDeleteData);
        //        GerpLog.Error(ErrorLocalization.ErrorWhileDeleteData + ", " + ex.Message);
        //        return Json(new { success = false });
        //    }
        //}

    }
}
